package com.ym.b2b.order.tk;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ym.b2b.buyer.dao.EmCBuyerAddressDao;
import com.ym.b2b.buyer.dao.EmCBuyerDao;
import com.ym.b2b.buyer.sqlbean.EmCBuyer;
import com.ym.b2b.buyer.sqlbean.EmCBuyerAddress;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.product.dao.EmKStorageDao;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.product.sqlbean.EmBProduct;
import com.ym.b2b.product.sqlbean.EmKStorage;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.sqlbean.EmCUser;

@Controller
@RequestMapping("/tk")
public class TkController extends BaseController {

    @Autowired
    private ProductDao         pdao;

    @Autowired
    private EmCBuyerDao        buyerDao;

    @Autowired
    private EmCUserDAO         userDao;

    @Autowired
    private EmKStorageDao      emKStorageDao;

    @Autowired
    private EmCBuyerAddressDao emCBuyerAddressDao;

    @RequestMapping("tb.s")
    @Transactional
    public void tb() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = TKDBFactroy.getTkCon();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            // 同步商品

            // 删除之前的记录
            emKStorageDao.deleteEmKStorage(new EmKStorage());
            pdao.deleteEmBProduct(new EmBProduct());
            emCBuyerAddressDao.deleteEmCBuyerAddress(new EmCBuyerAddress());

            buyerDao.deleteEmCBuyer(new EmCBuyer());

            // userDao.deleteEmCUser(new EmCUser());

            rs = stmt.executeQuery("select * from web_spkfk");
            while (rs.next()) {
                // 插入商品表
                EmBProduct emBProduct = new EmBProduct();
                emBProduct.setProductCode(rs.getString("spid"));
                if (null == pdao.showProductDetail(emBProduct)) {
                    // 新增
                    // System.out.println("新增商品：" + rs.getString("spid") + rs.getString("zjm"));
                    emBProduct.setProductCode(rs.getString("spid"));
                    emBProduct.setProductName(rs.getString("spmch"));
                    emBProduct.setChsName(rs.getString("spmch"));
                    emBProduct.setSpell(rs.getString("zjm") == null ? "无" : rs.getString("zjm"));
                    emBProduct.setPackingUnit(rs.getString("dw"));
                    emBProduct.setProducingName(rs.getString("shpchd"));
                    emBProduct.setProductSpec(rs.getString("shpgg"));
                    emBProduct.setAuthorizeNumber(rs.getString("pizhwh"));
                    emBProduct.setProductTypeName(rs.getString("shangplx"));
                    emBProduct.setPackingAmount(Long.parseLong(rs.getString("jlgg")));
                    emBProduct.setShopPrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setSalePrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setCashPrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setRetailPrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setMiddlePackPrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setControlSalePrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setFullSalePrice(Double.parseDouble(rs.getString("hshj")));
                    emBProduct.setApproveStatus(1);
                    emBProduct.setArrearsPrice(Double.parseDouble(rs.getString("hshj")));
                    pdao.insertEmBProduct(emBProduct);

                }
            }

            // 同步采购商
            rs = null;
            rs = stmt.executeQuery("select * from web_mchk");
            while (rs.next()) {
                // 插入商品表
                EmCBuyer emCBuyer = new EmCBuyer();
                emCBuyer.setBuyerCode(rs.getString("dwbh"));
                if (null == buyerDao.showEmCBuyerDetail(emCBuyer)) {
                    // 新增
                    emCBuyer.setChsName(rs.getString("dwmch"));
                    emCBuyer.setSpell(rs.getString("zjm"));
                    emCBuyer.setApproveStatus(1);
                    emCBuyer.setIsBranch(0);
                    buyerDao.insertBuyer(emCBuyer);

                    EmCBuyerAddress emCBuyerAddress = new EmCBuyerAddress();
                    emCBuyerAddress.setAddress(rs.getString("dwmch"));
                    emCBuyerAddress.setBuyerCode(rs.getString("dwbh"));
                    emCBuyerAddressDao.insertEmCBuyerAddress(emCBuyerAddress);

                    EmCUser user = new EmCUser();
                    user.setLoginName(rs.getString("dwbh"));
                    user.setLoginPwd("5556037DEE7990E63215CDEFC10FF78C");
                    user.setUserType(2);
                    user.setIsValid(1);
                    user.setExtendCode(rs.getString("dwbh"));
                    userDao.insertEmCUser(user);
                }
            }
            // 同步库存

            rs = null;
            rs = stmt.executeQuery("select * from web_spkfjc");
            while (rs.next()) {
                // 新增库存
                logger.info("新增库存：" + rs.getString("spid") + rs.getString("kcshl"));
                EmKStorage emKStorage = new EmKStorage();
                emKStorage.setProductCode(rs.getString("spid"));
                emKStorage.setAmount(Double.parseDouble(rs.getString("kcshl")));
                emKStorageDao.insertEmKStorage(emKStorage);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
